package com.sms.plateform.web.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.sms.plateform.common.finals.Constants;
import com.sms.plateform.common.page.PageList;
import com.sms.plateform.common.page.PageProperty;
import com.sms.plateform.common.util.JSONUtil;
import com.sms.plateform.common.util.Utils;
import com.sms.plateform.kernel.entity.Businesscard;
import com.sms.plateform.kernel.entity.BusinesscardGroup;
import com.sms.plateform.kernel.entity.MenuInfo;
import com.sms.plateform.kernel.entity.RoleInfo;
import com.sms.plateform.kernel.entity.UserInfo;
import com.sms.plateform.kernel.entity.UserRole;
import com.sms.plateform.kernel.service.IBusinesscardGroupService;
import com.sms.plateform.kernel.service.IBusinesscardService;
import com.sms.plateform.kernel.service.IMenuInfoService;
import com.sms.plateform.kernel.service.IRoleInfoService;

@Controller
public class BusinessCardController extends BaseController {
	
	@Autowired
	private IMenuInfoService menuInfoService;
	
	@Autowired
	private IRoleInfoService roleInfoService;
	
	@Autowired
	IBusinesscardService businesscardService;
	
	@Autowired
	IBusinesscardGroupService businesscardGroupService;
	
//	@RequestMapping("/business_card")
//	public ModelAndView indexHandel(HttpServletRequest request, HttpServletResponse response) {
//		Map<String, Object> param = new HashMap<String, Object>();
//		Map<String, Object> info = new HashMap<String, Object>();
//		String menucode=this.getUrlParam(request, "menucode");
//		
//		//获取角色和顶级菜单
//		UserRole userRole = (UserRole) this.getSessionObject(request, Constants.SESSION_ROLE);
//		Map<String, Object> topMenu = menuInfoService.getTopMenu(userRole);
//		info.put("topMenu", topMenu);
//		info.put("currentMenu", 20);
//		
//		//获取名片分组
//		UserInfo userInfo=this.getUserInfo(request);
//		Integer userid = userInfo.getId();
//		param.put("userid", userid);
//		List<BusinesscardGroup> cardGrouplist = businesscardGroupService.listBusinessCardGroupInfo(param);
//		info.put("groupList", cardGrouplist);
//		
//		return new ModelAndView("businesscard/business_card_home", "info", info);
//	}
	
//	@RequestMapping("/business_card_management")
//	public ModelAndView businessCardManagement(HttpServletRequest request, HttpServletResponse response) {
//		Map<String, Object> param = new HashMap<String, Object>();
//		Map<String, Object> info = new HashMap<String, Object>();
//		String groupid = request.getParameter("groupid");
//		info.put("srdm", new Date().getTime());//避免jQuary绑定干扰导致多次查询
//		if(groupid==null || groupid.equals("")){
//			info.put("current", "名片管理 -&gt; 全部");
//			return new ModelAndView("businesscard/business_card_management", "info", info);
//		} else {
//			if(!groupid.equals("nullgroup")){
//				param.put("id", groupid);
//				BusinesscardGroup groupinfo = businesscardGroupService.getModel(param);
//				String gname = groupinfo.getGroupname();
//				info.put("current", "名片管理 -&gt; "+gname);
//			} else {
//				info.put("current", "名片管理 -&gt; 未分组");
//			}
//			info.put("currentGroupId", groupid);
//			return new ModelAndView("businesscard/business_card_management", "info", info);
//		}
//	}
	
	@RequestMapping("/card_box_frame")
	public ModelAndView businessCardManagement(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> info = new HashMap<String, Object>();
		String id = request.getParameter("id");
		info.put("currentGroupId", id);
		return new ModelAndView("businesscard/business_card_management", "info", info);
	}
	
	@RequestMapping("/get_business_card_group_info")
	public ModelAndView getBusinessCardGroupInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, Object> param = new HashMap<String, Object>();
		UserInfo userInfo = (UserInfo) this.getSessionObject(request, Constants.SESSION_USER);
		Integer userid = userInfo.getId();
		param.put("userid", userid);
		List<BusinesscardGroup> cardGrouplist = businesscardGroupService.listBusinessCardGroupInfo(param);
		response.getWriter().write(JSONUtil.list2json(cardGrouplist));
		return null;
	}
	
	@RequestMapping("/business_card_addcard")
	public ModelAndView businessCardAddcard(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, Object> param = new HashMap<String, Object>();
		Map<String, Object> info = new HashMap<String, Object>();
		UserInfo userInfo = (UserInfo) this.getSessionObject(request, Constants.SESSION_USER);
		Integer userid = userInfo.getId();
		String newCardInGroup = request.getParameter("newCardInGroup");
		String cardname = request.getParameter("cardname");
		String mobilephone = request.getParameter("mobilephone");
		String phone = request.getParameter("phone");
		String sex = request.getParameter("sex");
		String birthday = request.getParameter("birthday");
		String company = request.getParameter("company");
		String post = request.getParameter("post");
		String mail = request.getParameter("mail");
		String married = request.getParameter("married");
		String zipcode = request.getParameter("zipcode");
		String address = request.getParameter("address");
		String remark1 = request.getParameter("remark1");
		String remark2 = request.getParameter("remark2");
		String remark3 = request.getParameter("remark3");
		String remark4 = request.getParameter("remark4");
		if(newCardInGroup != null && !newCardInGroup.equals("") && !newCardInGroup.equals("nogroup")){
			param.put("cardgroup", newCardInGroup);
		}
		param.put("userid", userid);
		param.put("cardname", cardname);
		param.put("mobilephone", mobilephone);
		param.put("phone", phone);
		param.put("sex", sex);
		param.put("birthday", birthday);
		param.put("company", company);
		param.put("post", post);
		param.put("mail", mail);
		param.put("married", married);
		param.put("zipcode", zipcode);
		param.put("address", address);
		param.put("remark1", remark1);
		param.put("remark2", remark2);
		param.put("remark3", remark3);
		param.put("remark4", remark4);
		String status = businesscardService.insertNewCard(param);
		
		response.getWriter().write(status);
		return null;
	}
	
	@RequestMapping("/business_card_ajax_list")
	public ModelAndView businessCardAjaxList(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String pageNo = request.getParameter("pageNo");
		String pageSize = request.getParameter("pageSize");
		String pageNum = "1";
		int pageSizeNum = Constants.PAGE_SIZE_DEFAULT;
		if (!"".equals(pageNo)) {
			pageNum = pageNo;
		}
		if (!"".equals(pageSize)) {
			pageSizeNum = Utils.parseInt(pageSize, 1);
		}
		PageProperty pp = new PageProperty();
		pp.setNpage(pageNum);
		pp.setNpagesize(Integer.toString(pageSizeNum));
		
		UserInfo userInfo = (UserInfo) this.getSessionObject(request, Constants.SESSION_USER);
		Integer userid = userInfo.getId();
		String cardgroup = request.getParameter("cardgroup");
		String searchitem = request.getParameter("searchitem");
		String searchparam = request.getParameter("searchparam");
		String cardname= null;
		String mobilephone= null;
		if(searchitem != null) {
			if(searchitem.equals("name")){cardname=searchparam;}else if(searchitem.equals("mobilephone")){mobilephone=searchparam;}
		}
		
		if (userid != null) {
			pp.putParamMap("userid", userid);
		}
		if (cardname != null && cardname.length() > 0) {
			pp.putParamMap("cardname", cardname);
		}
		if (mobilephone != null && mobilephone.length() > 0) {
			pp.putParamMap("mobilephone", mobilephone);
		}
		if (cardgroup != null && cardgroup.length() > 0) {
			if(cardgroup.equals("nogroup")){
				pp.putParamMap("cardgroupisnull", "yes");
			} else if(cardgroup.equals("all")){
				//all
			} else {
				pp.putParamMap("cardgroup", cardgroup);
			}
		}
		PageList<Map<String, Object>> cardlist = businesscardService.getBusinessCardList(pp);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("dataCount", cardlist.getTotalRecords());
		map.put("dataList", cardlist.getRecords());
		map.put("pageCount", cardlist.getTotalPages());
		response.getWriter().write(JSONUtil.map2json(map));
		return null;
	}
	
	@RequestMapping("/delete_business_card")
	public ModelAndView deleteBusinessCard(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, Object> param = new HashMap<String, Object>();
		UserInfo userInfo = (UserInfo) this.getSessionObject(request, Constants.SESSION_USER);
		Integer userid = userInfo.getId();
		if (userid != null) {
			param.put("userid", userid);
		}
		String ids = request.getParameter("id");
		if(ids == null || ids.length() == 0){return null;}
		String id[]=ids.split(",");
		for(int i = 0; i < id.length; i++){
			if(id[i] == "," || id[i].length() == 0){
				id[i]="";
			}
		}
		param.put("id", id);
		
		String state = businesscardService.deleteBusinessCardByIds(param);
		response.getWriter().write("success");
		return null;
	}
	
	@RequestMapping("/business_card_addcard_group")
	public ModelAndView businessCardAddcardGroup(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, Object> param = new HashMap<String, Object>();
		UserInfo userInfo = (UserInfo) this.getSessionObject(request, Constants.SESSION_USER);
		Integer userid = userInfo.getId();
		if (userid != null) {
			param.put("userid", userid);
		}
		String groupname = request.getParameter("groupname");
		param.put("groupname", groupname);
		
		String state = businesscardService.addBusinessCardGroup(param);
		return null;
	}
	
	@RequestMapping("/delete_business_card_group")
	public ModelAndView deleteBusinessCardGroup(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, Object> param = new HashMap<String, Object>();
		UserInfo userInfo = (UserInfo) this.getSessionObject(request, Constants.SESSION_USER);
		Integer userid = userInfo.getId();
		if (userid != null) {
			param.put("userid", userid);
		}
		String currentGroupId = request.getParameter("currentGroupId");
		param.put("id", currentGroupId);
		param.put("cardgroup", currentGroupId);
		
		String state = businesscardService.deleteBusinesscardGroupById(param);
		response.getWriter().write("success");
		return null;
	}
	
	@RequestMapping("/get_business_card_info")
	public ModelAndView getBusinessCardInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, Object> param = new HashMap<String, Object>();
		Map<String, Object> info = new HashMap<String, Object>();
		UserInfo userInfo = (UserInfo) this.getSessionObject(request, Constants.SESSION_USER);
		Integer userid = userInfo.getId();
		if (userid != null) {
			param.put("userid", userid);
		}
		String id = request.getParameter("id");
		param.put("id",id);
		Map<String, Object> cardInfo = businesscardService.getBusinessCardInfo(param);
		info.put("cardInfo", cardInfo);
//		System.out.println(JSONUtil.map2json(info));
		response.getWriter().write(JSONUtil.map2json(info));
		return null;
	}
	
	@RequestMapping("/business_card_editcard")
	public ModelAndView businessCardEditcard(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, Object> param = new HashMap<String, Object>();
		Map<String, Object> info = new HashMap<String, Object>();
		UserInfo userInfo = (UserInfo) this.getSessionObject(request, Constants.SESSION_USER);
		Integer userid = userInfo.getId();
		String id = request.getParameter("id");
		String newCardInGroup = request.getParameter("newCardInGroup");
		String cardname = request.getParameter("cardname");
		String mobilephone = request.getParameter("mobilephone");
		String phone = request.getParameter("phone");
		String sex = request.getParameter("sex");
		String birthday = request.getParameter("birthday");
		String company = request.getParameter("company");
		String post = request.getParameter("post");
		String mail = request.getParameter("mail");
		String married = request.getParameter("married");
		String zipcode = request.getParameter("zipcode");
		String address = request.getParameter("address");
		String remark1 = request.getParameter("remark1");
		String remark2 = request.getParameter("remark2");
		String remark3 = request.getParameter("remark3");
		String remark4 = request.getParameter("remark4");
		if(id != null || !id.equals("")){
			param.put("id", id);
		} else{
			return null;
		}
		if(newCardInGroup != null && !newCardInGroup.equals("") && !newCardInGroup.equals("nogroup")){
			param.put("cardgroup", newCardInGroup);
		} else {
			param.put("cardgroup", "null");
		}
		param.put("userid", userid);
		param.put("cardname", cardname);
		param.put("mobilephone", mobilephone);
		param.put("phone", phone);
		param.put("sex", sex);
		if(birthday != null && !birthday.equals("")){
			param.put("birthday", birthday);
		} else {
			param.put("birthday", null);
		}
		param.put("company", company);
		param.put("post", post);
		param.put("mail", mail);
		param.put("married", married);
		param.put("zipcode", zipcode);
		param.put("address", address);
		param.put("remark1", remark1);
		param.put("remark2", remark2);
		param.put("remark3", remark3);
		param.put("remark4", remark4);
		String status = businesscardService.updateBusinessCard(param);
		
		response.getWriter().write(status);
		return null;
	}
}